package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemEditListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2017/12/9.
 */

public class ProductInformationAdapter extends BaseAdapter /*implements ProduceReceiveOrderItemDetailActivity.onSignListener*/ {
    private Context context;
    private List<ProductBean> datas = new ArrayList<ProductBean>();
    private LayoutInflater inflater;
    private String TAG = "";
    private ItemRemoveListener itemRemoveListener;
    private ItemEditListener itemEditListener;


    public ProductInformationAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;
    }

    public void setData(List<ProductBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }
    }

    public void setRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    public void setItemEditListener(ItemEditListener itemEditListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public ProductBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_add_product_information_lv, null);
            holder.tv_up = convertView.findViewById(R.id.tv_up);
            holder.tv_down = convertView.findViewById(R.id.tv_down);
            holder.img_right = convertView.findViewById(R.id.img_right);
            holder.img_left = convertView.findViewById(R.id.img_left);

            if (TAG.equals("EngineeringInstallWorkOrderInfoForSuperFragment") || TAG.equals("EngineeringInstallationOrderActivity")) {
                holder.img_left.setVisibility(View.GONE);
                holder.img_right.setVisibility(View.VISIBLE);
            } else if (TAG.equals("ReceiveEngineeringConstructOrderActivity") || TAG.equals("EditEngineeringConstructOrderDetailActivity")) {
                holder.img_left.setVisibility(View.VISIBLE);
                holder.img_right.setVisibility(View.GONE);
            } else if (TAG.equals("EngineeringConstructOrderActivity") || TAG.equals("EngineeringConstructionOrderEditFragment")) {
                holder.img_left.setVisibility(View.VISIBLE);
                holder.img_right.setVisibility(View.VISIBLE);
            } else if (TAG.equals("ReceiveEngineeringInstallOrderActivity") || TAG.equals("EditEngineeringInstallationOrderDetailActivity")) {
                holder.img_left.setVisibility(View.GONE);
                holder.img_right.setVisibility(View.GONE);
            }

            if (TAG.equals("ViewEngineeringInstallOrderDetailActivity") || TAG.equals("ViewEngineeringConstructOrderDetailActivity") || TAG.equals("TeamEngineeringInstallOrderFeedBackActivity")
                    || TAG.equals("WorkOrderInfomationForEngineeringInstallFragment")) {
                holder.img_left.setVisibility(View.GONE);
                holder.img_right.setVisibility(View.GONE);
            }

            if (TAG.equals("ProductionOrderEditFragment") || TAG.equals("EngineeringInstallationOrderEditFragment") || TAG.equals("EngineeringConstructionOrderEditFragment")) {
                holder.img_right.setBackgroundResource(R.mipmap.icon_edit);
            }

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ProductBean bean = datas.get(position);
        if (bean != null) {

            String picu = bean.getPicUrl() + "";
            String str = picu.replace("[", "").replace("]", "");

            if (null != bean.getPicUrl()) {
                Glide.with(context).load(new File(str)).error(R.mipmap.img_product).into(holder.img_left);
            }

            holder.tv_up.setText(bean.getName());

            CommonEnum.ProductShapeType shape = bean.getType();
            if (null != shape) {
                if (shape == CommonEnum.ProductShapeType.ROUND) {
                    // holder.tv_down.setText(bean.getDiameter() + "*" + bean.getThick() + "*" + bean.getBorder());
                    holder.tv_down.setText("直径" + bean.getDiameter() + "mm*" + "厚度" + bean.getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                    // holder.tv_down.setText(bean.getHeight() + "*" + bean.getWidth() + "*" + bean.getThick());
                    holder.tv_down.setText("长度" + bean.getWidth() + "mm*" + "高度" + bean.getHeight() + "mm*" + "厚度" + bean.getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                    holder.tv_down.setText(bean.getDesc());
                }
            }
        }

        if (TAG.equals("ProductionOrderEditFragment") || TAG.equals("EngineeringConstructionOrderEditFragment") || TAG.equals("EngineeringInstallationOrderEditFragment")) {
            holder.img_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemEditListener != null) {
                        itemEditListener.edit(position);
                    }
                }
            });
        } else {
            holder.img_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemRemoveListener != null) {
                        itemRemoveListener.remove(position);
                    }
                }
            });
        }
        return convertView;
    }

    static class ViewHolder {
        private TextView tv_up;
        private TextView tv_down;
        private ImageView img_right;
        private ImageView img_left;
    }

}
